package com.skp.shaphan.savedConnections;

import java.util.Enumeration;
import java.util.Properties;

public class SavedConnection {
	private String name;
	private String type;
	private String typeClass;
	private int index;  /* so we can load properties, etc. later */
	private boolean propertiesLoaded = false;
	private Properties properties;
	private int id;
	private boolean newConnection;
	
	public SavedConnection() {
		newConnection = false;
	}
	public SavedConnection(boolean newConnection) {
		this.newConnection = true;
		this.name = "<New>";
	}
	public SavedConnection(int index, int id, String name, String type) {
		this.newConnection = false;
		this.name = name;
		this.type = type;
		this.index = index;
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	private void loadProperties() {
		SavedConnections sc = SavedConnections.getInstance();
		this.properties = sc.getConnectionProperties(index);
		propertiesLoaded = true;
	}
	public void save() {
		System.out.println("---- Saving Connection ------");
		System.out.println("Connection Name: " + this.getName());
		SavedConnections sc = SavedConnections.getInstance();
		sc.updateConnection(this);
		System.out.println("Properties loaded: " + propertiesLoaded);
		if(propertiesLoaded) {
			sc.updateConnectionProperties(this, properties);
		}
	}
	public Properties getProperties() {
		if(propertiesLoaded == false) {
			loadProperties();
		}
		return this.properties;
	}
	public void updateProperties(Properties newProperties) {
		if(propertiesLoaded == false) {
			loadProperties();
		}
		Enumeration<Object> keys = newProperties.keys();
		while(keys.hasMoreElements()) {
			Object key = keys.nextElement();
			this.properties.put(key, newProperties.get(key));
		}
	}
	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setTypeClass(String typeClass) {
		this.typeClass = typeClass;
	}
	public String getTypeClass() {
		return typeClass;
	}
}
